// pages/addSchedule/index.js
const app = getApp()
import { getScheduleAdd } from '../../api/schedule.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    name:'',//参赛名称
    nameerr:false,
    date: '',//开赛日期
    dateerr:false,
    shortAddress: [],
    multiIndex: [0, 0, 0],
    multiArray: [],
    objectMultiArray:[],
    scheduleId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = JSON.parse(options.user)
    this.setData({
      id: user.id,
      name: user.matchName,
      date: user.day,
      scheduleId: user.scheduleId,
      date1: app.timeFn(user.day, '-', false)
    })
    app.getIndex(user.shortAddress.split(','), e => {
      this.setData({ ...app._processingAreaData(e) })
    })
  },
  bindMultiPickerColumnChange(e) {
    let multiIndex = this.data.multiIndex
    multiIndex[e.detail.column] = e.detail.value
    this.setData({ ...app._processingAreaData(multiIndex) })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindReplaceName: function (e) {
    this.setData({
      name: e.detail.value
    })
    this.setData({nameerr: this.data.name == null || this.data.name == ''})
  },
  //日期
  bindDateChange: function (e) {
    this.setData({ date: e.detail.value })
    this.check()
  },
  check() {
    let result2 = this.error(this.data.date, 'dateerr')
    let result3 = this.error(this.data.name, 'nameerr')
    this.setData({ buttoncolor: result2 == "1" && result3 == "1" })
  },
  error(value, valueerr) {
    if (value == null || value == '') {
      this.setData({
        valueerr: true
      })
      return "0"
    } else {
      this.setData({
        valueerr: false
      })
      return "1"
    }
  },
  submit() {
    let { id,name, date, multiArray, multiIndex, scheduleId } = this.data
    let result2 = this.error(date, 'dateerr')
    let result3 = this.error(name, 'nameerr')
    if (result2 == "1" && result3 == "1") {
      date = date.replace(/-/g, '/')
      let address = '';
      if (multiArray[0][multiIndex[0]]) {
        address += multiArray[0][multiIndex[0]]
      }
      if (multiArray[1][multiIndex[1]]) {
        address += (',' + multiArray[1][multiIndex[1]])
      }
      if (multiArray[2][multiIndex[2]]) {
        address += (',' + multiArray[2][multiIndex[2]])
      }
      let obj = {
        matchId: 0,
        scheduleId: scheduleId,
        matchName: name,
        timeStart: date,
        shortAddress: address
      }
      getScheduleAdd(obj).then(res => {
        if (res.code == 0) {
          wx.showModal({
            title: '修改成功',
            content: '是否返回',
            showCancel: true,//是否显示取消按钮
            cancelText: "否",//默认是“取消”
            cancelColor: '#999999',//取消文字的颜色
            confirmText: "是",//默认是“确定”
            confirmColor: '#000000',//确定文字的颜色
            success:res=> {
              if (res.cancel) {
                //点击取消,默认隐藏弹框
              } else {
                //点击确定
                wx.redirectTo({
                  url: '/pages/schedule_detail/index?id='+id,
                })
              }
            }
          })
        } else {
          wx.showToast({title: '修改失败',icon:'none'})
        }
      })
    } else {
      this.setData({
        buttoncolor: false
      })
    }
  }
})